package com.wlh.animation.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

/**
 * Created by weilh on 2017/7/3.
 */

public class BottomMenuPopWindow extends PopupWindow{
    private MaxItemListView listView;
    public BottomMenuPopWindow(Context context) {
        super(context);
        setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setFocusable(true);
        setOutsideTouchable(false);

        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

        listView = new MaxItemListView(context);
        listView.setBackgroundColor(Color.WHITE);
        ListView.LayoutParams layoutParams = new ListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        listView.setLayoutParams(layoutParams);

        linearLayout.setGravity(Gravity.BOTTOM);
        linearLayout.addView(listView);

        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissWithAnim();
            }
        });

        setContentView(linearLayout);
    }


    public MaxItemListView getListView() {
        return listView;
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    private void dismissWithAnim() {
        listView.post(new Runnable() {
            @Override
            public void run() {
                TranslateAnimation translateAnimation = new TranslateAnimation(0 ,0,0,
                        listView.getMeasuredHeight());
                translateAnimation.setDuration(400);
                listView.startAnimation(translateAnimation);
                translateAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        dismiss();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
            }
        });
    }
    private void startAnimation() {
        listView.post(new Runnable() {
            @Override
            public void run() {
                TranslateAnimation translateAnimation = new TranslateAnimation(0 ,0, listView.getMeasuredHeight(), 0);
                translateAnimation.setDuration(400);
                listView.startAnimation(translateAnimation);
            }
        });
    }

    @Override
    public void showAsDropDown(View anchor) {
        super.showAsDropDown(anchor);
        startAnimation();
    }
}
